/*
*Copyright © 2019 XSNOW
*四川瑞韬科技有限公司
*http://www.ratotech.com.cn
*All rights reserved.
*/
package com.icesoft.xsnow.upms.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.icesoft.xsnow.common.core.constant.CommonConstants;
import com.icesoft.xsnow.common.security.dto.XSnowUser;
import com.icesoft.xsnow.common.security.util.SecurityUtils;
import com.icesoft.xsnow.framework.api.reponses.R;
import com.icesoft.xsnow.framework.api.util.ApiAssert;
import com.icesoft.xsnow.framework.controller.BaseController;
import com.icesoft.xsnow.upms.api.param.RegisterShopParam;
import com.icesoft.xsnow.upms.api.po.EcShop;
import com.icesoft.xsnow.upms.core.enums.ShopEcodeEnum;
import com.icesoft.xsnow.upms.service.EcShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @program: xsnow
 * @description: 商铺信息 前端控制器
 * @author xuefeng.gao
 * @create: 2019-05-28
 */
@RestController
@Validated
@RequestMapping(value = "ec/shop", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(tags = {"EcShop"}, description = "商铺信息相关接口")
public class EcShopController extends BaseController {
    @Autowired
    private  EcShopService ecShopService;

    @ApiOperation("外部应用注册商铺信息")
    @PostMapping("/client")
    @PreAuthorize("@pms.hasPermission('ec:shop:register')" )
    public R register(@RequestBody RegisterShopParam shopParam) {
        XSnowUser user = SecurityUtils.getUser();
        String clientId = user.getTenantId();
        ApiAssert.isNull(ShopEcodeEnum.CLIENT_NOT_FOUND,clientId);
        EcShop ecShop = shopParam.convert(EcShop.class);
        ecShop.setTenantId(clientId);
        ecShop.setStatus(CommonConstants.STATUS_NORMAL);
        return success(ecShopService.save(ecShop));
    }


    @ApiOperation("外部应用修改商铺信息" )
    @PutMapping("/client")
    @PreAuthorize("@pms.hasPermission('ec:shop:edit')" )
    public R updateById(@RequestBody RegisterShopParam shopParam) {
        EcShop shop = ecShopService.getById(shopParam.getId());
        //商铺是否存在
        ApiAssert.isNull(ShopEcodeEnum.SHOP_NOT_FOUND,shop);
        //商铺是否锁定
        ApiAssert.isTrue(ShopEcodeEnum.SHOP_STATUS_LOCK,CommonConstants.STATUS_NORMAL.equals(shop.getStatus()));
        BeanUtil.copyProperties(shop,shopParam);
        return success(ecShopService.updateById(shop));
    }

    @ApiOperation("新增商铺信息")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('ec:shop:add')" )
    public R save(@RequestBody EcShop ecShop) {
        return success(ecShopService.save(ecShop));
    }


    @ApiOperation("外部应用通过clientId 查询商铺信息")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "客户端ID", required = true, paramType = "path") })
    @GetMapping("/client/{id}" )
    public R getByClientId(@PathVariable("id" ) Integer clientId) {
        EcShop shop = ecShopService.getOne(Wrappers.<EcShop>lambdaQuery().eq(EcShop::getTenantId,clientId));
        return success(shop);
    }


    @ApiOperation(" 通过id查询商铺信息")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "商铺ID", required = true, paramType = "path") })
    @GetMapping("/{id}" )
//    @PreAuthorize("@pms.hasPermission('IS_AUTHENTICATED_ANONYMOUSLY')")
//    @PreAuthorize("hasRole('ROLE_ANONYMOUS')")
    public R getById(@PathVariable("id" ) Integer id) {
        XSnowUser user = SecurityUtils.getUser();
        String clientId = user.getTenantId();
        return success(ecShopService.getById(id));
    }

    @ApiOperation("分页查询商铺信息")
    @GetMapping("/page")
    public R page() {
        return success(ecShopService.page(this.getPage()));
    }


    @ApiOperation("删除商铺信息" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('ec:shop:del')" )
    public R removeById(@PathVariable Integer id) {
        return success(ecShopService.removeById(id));
    }

}
